<?php 
class GoodsModel extends Model{
	public $table = 'goods';
	public $validate = array( array('gname', 'nonull', '商品名称不能为空', 2, 3));
	public $auto = array( array('addtime', 'strtotime', 'function', 2, 3), array('index_image', '_index_image', 'method', 2, 3), array('list_image', '_list_image', 'method', 2, 3));
	//首页图片 自动完成处理
	public function _index_image($v) {
		$upload = new Upload();
		$file = $upload -> upload('index_image');
		return empty($file) ? $v : $file[0]['path'];
	}
	//列表页图片 自动完成处理
	public function _list_image($v) {
		$upload = new Upload();
		$file = $upload -> upload('list_image');
		return empty($file) ? $v : $file[0]['path'];
	}
	//添加商品
	public function addGoods() {
		if ($this -> create()) {
			if ($gid = $this -> add()) {
				//设置详情页图片
				$this -> setConImage($gid);
				//添加商品的库存
				$this -> setGoodsSpec($_POST['cid'], $gid);
				//添加商品的属性
				$this -> setGoodsAttr($_POST['cid'], $gid);
				
				return true;
			}
		}
	}
	//设置商品库存
	private function setGoodsSpec($cid,$gid){
		$db = M('stock');
		//清除旧库存信息
		$db->where('gid='.$gid)->del();
		//如果没有库存信息
		if(!isset($_POST['spec']))return;
		$spec= $_POST['spec'];
		//向商品属性列表中插入的数据
		$goodsAttrList=array();
		foreach($spec['price'] as $id=>$price){
			//价格、库存、货号，有任何一个没填，都不添加（此规格忽略）
			if(empty($price) || empty($spec['stock'][$id]) || empty($spec['sn'][$id])){
				continue;
			}
			$data=array(
				'gid'=>$gid,//商品id
				'price'=>$price,//商品价格
				'stock'=>$spec['stock'][$id],//库存
				'sn'=>$spec['sn'][$id],//货号
			);
			if(isset($spec['sid']) && !empty($spec['sid'][$id])){
				$data['sid']=$spec['sid'][$id];
			}
			//向库存表中插入数据
			if($sid= $db->add($data)){
				//向库存属性表插入数据
				$stockData=array();
				foreach($spec['attr'] as $attr){
					$tmp=explode('-',$attr[$id]);
					$stockData['avid']=$tmp[0];
					$stockData['sid']=$sid;//商品gid
					$stockData['gid']=$gid;//商品gid
					$stockData['cid']=$cid;//栏目cid
					$db->table('stock_attr')->add($stockData);
					//将规格属性
					$_POST['attr']['select-'.$tmp[0]]=$attr[$id];
				}
				
			}
		}
		$_POST['attr'] = array_unique($_POST['attr']);
		return;
	}
	//设置详情页图片
	private function setConImage($gid) {
		//处理内容页图片
		$upload = new Upload();
		$images = $upload -> upload('image');
		$imageObj = new Image();
		$data = array();
		foreach ($images as $img) {
			//小图
			$tmp['small'] = $imageObj -> thumb($img['path'], $img['dir'] . $img['filename'] . '_small.' . $img['ext'], 50, 50);
			//中图
			$tmp['medium'] = $imageObj -> thumb($img['path'], $img['dir'] . $img['filename'] . '_medium.' . $img['ext'], 350, 350);
			//大图
			$tmp['big'] = $img['path'];
			$tmp['gid'] = $gid;
			M('goods_image') -> add($tmp);
		}
	}

	//修改商品
	public function editGoods() {
		if ($this -> create()) {
			if ($this -> save()) {
				//设置详情页图片
				$this -> setConImage($_POST['gid']);
				//添加商品的库存
				$this -> setGoodsSpec($_POST['cid'], $_POST['gid']);
				//添加商品的属性
				$this -> setGoodsAttr($_POST['cid'], $_POST['gid']);
				return true;
			}
		}
	}

	//添加商品属性
	public function setGoodsAttr($cid, $gid) {
		$data = $this -> getAttrData();
		if (empty($data))
			return;
		$db = M('goods_attr_list');
		//删除商品所有旧的属性
		$db -> where("gid=$gid") -> del();
		foreach ($data as $d) {
			$d['cid'] = $cid;
			$d['gid'] = $gid;
			$db -> add($d);
		}
		return true;
	}
	//把post过来的属性，变成一个纺一数据类型（因为由于 表单形态不一有input 有checkbox）
	private function getAttrData() {
		//没有属性的商品，就不做了。。
		if (empty($_POST['attr']))
			return;
		$attr = $_POST['attr'];
		$data = array();
		$id = 0;
		foreach ($attr as $name => $value) {
			$info = explode('-', $name);
			//$info[0] 类型如radio,checkbox
			switch($info[0]) {
				case 'radio' :
				case 'checkbox' :
				case 'select' :
					$t = explode('-', $value);
					$data[$id]['avid'] = $t[0];
					$data[$id]['attr_name'] = $t[1];
					break;
				case 'text' :
					$data[$id]['avid'] = $info[2];
					$data[$id]['attr_name'] = $value;
					break;
			}
			$id++;
		}
		return $data;
	}

}



 ?>